package com.huang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.base.BaseController;
import com.huang.entity.UserPo;
import com.huang.resp.CommonResult;
import com.huang.service.UserService;
import com.huang.vo.Pager;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * 用户管理模块
 *
 * @author han
 * @since 2022-05-26 22:40:08
 */
@Log4j2
@RestController
@RequestMapping("user")
public class UserController extends BaseController {

	@Resource
	private UserService userService;

	@GetMapping("list")
	public CommonResult list(@RequestBody Pager<UserPo> pager) {
		log.info("用户管理列表入参：{}", pager);
		QueryWrapper<UserPo> queryWrapper = new QueryWrapper<>();
		pager = (Pager<UserPo>) parse(pager, queryWrapper);
		pager = userService.page(pager, queryWrapper);
		return CommonResult.success(pager);
	}

	@PostMapping("add")
	public CommonResult add(@Validated @RequestBody UserPo userPo) {
		boolean save = userService.save(userPo);
		return save ? CommonResult.success() : CommonResult.error("保存用户失败");
	}

	@PostMapping("update")
	public CommonResult update(@Validated @RequestBody UserPo userPo) {
		boolean update = userService.updateById(userPo);
		return update ? CommonResult.success() : CommonResult.error("修改用户失败");
	}

	@PostMapping("delete")
	public CommonResult delete(String ids) {
		boolean remove = userService.removeByIds(Collections.singletonList(ids));
		return remove ? CommonResult.success() : CommonResult.error("删除用户失败");
	}

}
